package code.wyc.springboot_ysgz;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import code.wyc.dto.User;
import code.wyc.service.UserService;

@RestController
@RequestMapping(value="/user")
public class UserController {

	@Autowired
	UserService userService;
	ObjectMapper mapper = new ObjectMapper();
	
	@RequestMapping(value="/findByName/{name}",method=RequestMethod.GET
			,produces="text/html;charset=UTF-8")
	@ResponseBody
	public String findByname(@PathVariable("name") String name) {
		String nString = null;
		try {
			User user = userService.getUserByName(name);
			nString = (null!=user)?mapper.writeValueAsString(user):"无此用户";
		} catch (JsonProcessingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return nString;
	}
	
	@RequestMapping(value="/findUserCount",method=RequestMethod.GET)
	@ResponseBody
	public Integer findUserCount() {
		return userService.findUserCount();
	}
}
